package com.jihui.security.web.userdetails;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * web security basic configurer
 *
 * @author linux_china
 */
@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public abstract class JihuiWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;



    @Value("${info.account.login.url}")
    private String loginUrl;

    @Value("${info.account.logout.url}")
    private String logoutUrl;
    @Value("${info.domain}")
    private String domain;




    /**
     * white urls for static resources
     */
    public String[] whiteUrls = new String[]{"/login/**","/webjars/**", "/static/**", "/static/css/**", "/static/images/**", "/static/js/**","/jsondoc","/jsondoc-ui.html","**/*.css","/webjars/**","**/*.js", "**/*.map", "*.html"};
    /**
     * remember me key for salt
     */
    protected static String rememberMeAppKey = "jihui_001";

    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().authorizeRequests()
                .antMatchers(whiteUrls).permitAll()
                .antMatchers(loginUrl).permitAll()
                .and()
                .csrf().disable()
                .httpBasic().disable().anonymous().disable()
                .formLogin()
                .loginPage(loginUrl)
                .permitAll()
                .and()
                .logout()
                .logoutUrl(logoutUrl)
                .logoutSuccessUrl("http://"+domain)
                .permitAll()
                .and()
                .rememberMe().key(rememberMeAppKey)
                .and()
                .exceptionHandling().authenticationEntryPoint(myLoginUrlAuthenticationEntryPoint())
                .accessDeniedHandler(restAccessDeniedHandler());
                internalConfigure(http);
    }

    /**
     * internal configure for http security, main: antMatchers("/home").authenticated()
     *
     * @param http http security
     * @throws Exception 配置异常
     */
    protected abstract void internalConfigure(HttpSecurity http) throws Exception;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }
    @Bean
    @ConditionalOnMissingBean(MyLoginUrlAuthenticationEntryPoint.class)
    public MyLoginUrlAuthenticationEntryPoint myLoginUrlAuthenticationEntryPoint(){
        return new MyLoginUrlAuthenticationEntryPoint("/",loginUrl,getRedirect());
    }
    @Bean
    @ConditionalOnMissingBean(RestAccessDeniedHandler.class)
    public RestAccessDeniedHandler restAccessDeniedHandler(){
        return new RestAccessDeniedHandler(loginUrl,getRedirect());
    }
    protected abstract String getRedirect();

}
